package com.kokomi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kokomi.handler.Assert;
import com.kokomi.pojo.DTO.LoginRequestDTO;
import com.kokomi.pojo.DTO.RegisterRequestDTO;
import com.kokomi.pojo.RedisConst;
import com.kokomi.pojo.User;
import com.kokomi.pojo.enumeration.BusinessCode;
import com.kokomi.pojo.enumeration.SystemCode;
import com.kokomi.pojo.enumeration.UserStatus;
import com.kokomi.service.UserService;
import com.kokomi.mapper.UserMapper;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * @author klioe
 * @description 针对表【t_user】的数据库操作Service实现
 * @createDate 2025-04-01 11:35:16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    StringRedisTemplate redisTemplate;


    @Override
    public boolean isAccountExist(String account) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        User user = userMapper.selectOne(queryWrapper);
        return user!=null;
    }

    @Override
    public boolean isMobileExist(String mobile) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        User user = userMapper.selectOne(queryWrapper);
        return user!=null;
    }

    @Override
    public ResponseEntity register(RegisterRequestDTO dto) {
        // 1.判断账号是否存在
        Assert.error(isAccountExist(dto.getAccount()), BusinessCode.USER_IS_EXIST);

        // 2.判断手机号是否存在
        Assert.error(isMobileExist(dto.getMobile()), BusinessCode.MOBILE_IS_EXIST);

        // 3.判断验证码是否失效
        String code = redisTemplate.opsForValue().get(RedisConst.EMAIL_ADDRESS_PREFIX+ ":"+ dto.getEmail());
        Assert.error(code == null, BusinessCode.VERIFICATION_ERROR);

        // 4.判断验证码是否正确
        Assert.error(!dto.getValidCode().equals(code), BusinessCode.VERIFICATION_ERROR);
//         5.入库
//         5.1 密码加密
        String password = BCrypt.hashpw(dto.getPassword(), BCrypt.gensalt());
        dto.setPassword(password);
        User user = new User();
        BeanUtils.copyProperties(dto, user);
        int insert = userMapper.insert(user);

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", dto.getEmail());
        User currentUser = userMapper.selectOne(queryWrapper);

        return ResponseEntity.status(SystemCode.SUCCESS.getCode()).body(currentUser);
    }

    @Override
    public ResponseEntity<?> login(LoginRequestDTO loginRequestDTO) {
        // 0.判断验证码
        String key = loginRequestDTO.getKey();
        Object o = redisTemplate.opsForValue().get(key);
        // 0.1 判断验证码是否过期

        Assert.error(o == null, BusinessCode.INVALID_VERIFICATION);
        // 0.2 判断验证码是否正确

        Assert.error(!o.equals(loginRequestDTO.getCode()), BusinessCode.VERIFICATION_ERROR);

        // 1.根据用户名查询用户是否存在
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount, loginRequestDTO.getAccount());
        User user = userMapper.selectOne(queryWrapper);

        Assert.error(user == null, BusinessCode.USER_NOT_EXIST);

        // 2.如果存在则验证密码(加密-->密文)是否正确
        boolean checkpw = BCrypt.checkpw(loginRequestDTO.getPassword(), user.getPassword());

        Assert.error(!checkpw, BusinessCode.PASSWORD_ERROR);

        // 3.如果密码正确，判断用户状态是否正常
        Integer state = user.getState();
        Integer activeKey = UserStatus.ACTIVE.getKey();
        Assert.error(state == null || !state.equals(activeKey), BusinessCode.USER_FROZEN_ERROR);

        // 4.如果合法，则返回用户信息

        return ResponseEntity.ok(user);
    }

    @Override
    public boolean isEmailExist(String Email) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", Email);
        User user = userMapper.selectOne(queryWrapper);
        return user!=null;
    }

}




